package queuestack;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author justnow
 * Created on 2020-10-03
 * Description
 * 用队列来实现栈
 */
public class Leetcode225_queuetostack {
    private Queue<Integer> queue1;
    private Queue<Integer> queue2;
    /** Initialize your data structure here. */
    public Leetcode225_queuetostack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    /** Push element x onto stack. */
    public void push(int x) {
        queue1.offer(x);
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        int res;
        while (queue1.size() > 1) {
            queue2.offer(queue1.poll());
        }
        res = queue1.poll();
        Queue<Integer> temp = queue1;
        queue1 = queue2;
        queue2 = temp;
        return res;
    }

    /** Get the top element. */
    public int top() {
        int res;
        while (queue1.size() > 1) {
            queue2.offer(queue1.poll());
        }
        res = queue1.peek();
        queue2.offer(queue1.poll());
        Queue<Integer> temp = queue1;
        queue1 = queue2;
        queue2 = temp;
        return res;
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        if (queue1.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }
}
